package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Migrate;
import com.zyd.shiro.business.entity.User;
import com.zyd.shiro.business.service.PakMigrateService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakMigrate;
import com.zyd.shiro.persistence.beans.SysUser;
import com.zyd.shiro.persistence.mapper.PakMigrateMapper;
import com.zyd.shiro.persistence.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakMigrateServiceImpl implements PakMigrateService {

    @Autowired
    private PakMigrateMapper pakMigrateMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Migrate insert(Migrate migrate) {
        Assert.notNull(migrate, "对象不可为空！");
        PakMigrate pakMigrate = migrate.getPakMigrate();
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(SecurityUtils.getSubject().getPrincipal()) ;
        pakMigrate.setUser(sysUser.getId());
        pakMigrate.setDept(sysUser.getDept());
        pakMigrate.setUorg(sysUser.getOrg());
        pakMigrate.setUnicode("ZY"+System.currentTimeMillis());
        pakMigrate.setAppstatus(0);
        pakMigrate.setUpdateTime(new Date());
        pakMigrate.setCreateTime(new Date());
        pakMigrateMapper.insertSelective(pakMigrate);
        return migrate;
    }

    @Override
    public void insertList(List<Migrate> migrates) {
            for(Migrate migrate:migrates){
               migrate.getPakMigrate().setUpdateTime(new Date());
               migrate.getPakMigrate().setCreateTime(new Date());
               pakMigrateMapper.insertSelective(migrate.getPakMigrate());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakMigrateMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Migrate migrate) {
        Assert.notNull(migrate, "对象不可为空！");
        migrate.setUpdateTime(new Date());
        return pakMigrateMapper.updateByPrimaryKey(migrate.getPakMigrate()) > 0;
    }

    @Override
    public boolean updateSelective(Migrate migrate) {
        Assert.notNull(migrate, "对象不可为空！");
        migrate.setUpdateTime(new Date());
        return pakMigrateMapper.updateByPrimaryKeySelective(migrate.getPakMigrate()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Migrate getByPrimaryKey(Long primaryKey) {
        PakMigrate pakMigrate = pakMigrateMapper.selectByPrimaryKey(primaryKey);
        return null == pakMigrate ? null : new Migrate(pakMigrate);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Migrate getOneByEntity(Migrate test) {
        Assert.notNull(test, "对象不可为空！");
        PakMigrate pakMigrate = pakMigrateMapper.selectOne(test.getPakMigrate());
        return null == pakMigrate ? null : new Migrate(pakMigrate);
    }

    @Override
    public List<Migrate> listAll() {
        List<PakMigrate> pakMigrates = pakMigrateMapper.selectAll();
        if (CollectionUtils.isEmpty(pakMigrates)) {
            return null;
        }
        List<Migrate> migrates = new ArrayList<>();
        for (PakMigrate pakMigrate : pakMigrates) {
            migrates.add(new Migrate(pakMigrate));
        }
        return migrates;
    }

    @Override
    public List<Migrate> listByEntity(Migrate entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Migrate> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakMigrate> pakMigrates = pakMigrateMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakMigrates)) {
            return null;
        }
        List<Migrate> migrates = new ArrayList<>();
        for (PakMigrate pakMigrate : pakMigrates) {
            migrates.add(new Migrate(pakMigrate));
        }
        PageInfo bean = new PageInfo<PakMigrate>(pakMigrates);
        bean.setList(migrates);
        return bean;
    }
@Override
    public void increase(PakMigrate pakMigrate) {
        pakMigrateMapper.insert(pakMigrate);
    }




}
